﻿using System;
using System.Linq;
using Core.Contract.DataContract;
using www.Models;

namespace Data.DAO
{
    public class LinkRefDAO//:BaseDAO<RELAXEntities,RelaxLink>
   {
       #region value
       private readonly RelaxDataContext _context = new RelaxDataContext();
       #endregion

       #region Hàm lấy dữ liệu
       public IQueryable<RelaxLinkDataContract>GetLinkDataContracts(int numrecord=0)
       {
           var lReturn = from l in _context.LINK_REFs
               join v in _context.LINK_VALUEs on l.id equals v.linkid
               select new RelaxLinkDataContract
               {
                   Id = Convert.ToInt32(l.id),
                   Name = v.name,
                   Keyword = v.keyword,
                   Decription = v.decription,
                   Link = l.link,
                   Target = l.target,
                   Ord = Convert.ToInt32(l.ord),
                   Active = Convert.ToInt32(l.active),
                   Languagecode = Convert.ToInt32(v.languagecode)
               };
           if (numrecord > 0)
               lReturn = lReturn.OrderByDescending(p=>p.Id).Take(numrecord);
           return lReturn;
       }
       #endregion
       #region Add
       public void Add(LINK_REF link)
       {
           _context.LINK_REFs.InsertOnSubmit(link);
           _context.SubmitChanges();

       }
       #endregion
       #region Update

       public void Update(LINK_REF link)
       {
           var first = _context.LINK_REFs.FirstOrDefault(p => p.id == link.id);
           if (first != null)
           {
               first.link = link.link;
               first.target = link.target;
               first.ord = link.ord;
               first.active = link.active;
               _context.SubmitChanges();
           }
       }
       #endregion

       #region Delete

       public void Delete(long iId)
       {
           var first = _context.LINK_REFs.FirstOrDefault(p => p.id == iId);
           if (first != null)
           {
               _context.LINK_REFs.DeleteOnSubmit(first);
               _context.SubmitChanges();
           }
       }
       #endregion
   }
}
